package com.atguigu.distributed.lock.leecode;

import java.util.ArrayList;
import java.util.Collections;

/**
 * 力扣中等151
 */
public class ReverStringWord {
    public String reverseWords(String s) {
        char[] splits = s.trim().toCharArray();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < splits.length; i++) {
            if(splits[i]!=' '){
                sb.append(splits[i]);
            }else if(sb.charAt(sb.length()-1)!=' '){
                sb.append(splits[i]);
            }
        }
        String[] splits2 = sb.toString().split(" ");
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < splits2.length; i++) {
            list.add(splits2[i]);
        }
        Collections.reverse(list);
        String res = "";
        for (int i = 0; i < list.size(); i++) {
//            if((i+1)<list.size()){
//                if((!" ".equals(list.get(i)))&&" ".equals(list.get(i+1))){
//                    res+=list.get(i);
//                    continue;
//                }
//                if((" ".equals(list.get(i)))&&(" ".equals(list.get(i+1)))){
//                    continue;
//                }
//            }

            if(i!=list.size()-1){
                res+=list.get(i)+" ";
            }else {
                res+=list.get(i);
            }

        }
        return res;

    }
}
